package com.liy.cloud.common.pw.utils;

/**
 * @author libs
 * @date 2019/3/27 15:25:34
 * @description 数字工具
 */
public class DigitUtil {

    public static<T extends Number> T getValue(Object o, Class<T> tClass) throws ClassCastException {
        return getValue(o, tClass, 0);
    }

    public static<T extends Number> T getValue(Object o, Class<T> tClass, Number nullValue) throws ClassCastException {

        if(o == null || o instanceof Number){
            Number number = (Number) o;
            if(o == null){
                number = nullValue;
            }
            if(Byte.class.equals(tClass)){
                return tClass.cast(number.byteValue());
            }
            if(Short.class.equals(tClass)){
                return tClass.cast(number.shortValue());
            }
            if(Integer.class.equals(tClass)){
                return tClass.cast(number.intValue());
            }
            if(Long.class.equals(tClass)){
                return tClass.cast(number.longValue());
            }
            if(Float.class.equals(tClass)){
                return tClass.cast(number.floatValue());
            }
            if(Double.class.equals(tClass)){
                return tClass.cast(number.doubleValue());
            }
            return tClass.cast(number);
        }
        if(o instanceof String){
            String str = (String) o;
            if(Byte.class.equals(tClass)){
                return tClass.cast(Byte.parseByte(str));
            }
            if(Short.class.equals(tClass)){
                return tClass.cast(Short.parseShort(str));
            }
            if(Integer.class.equals(tClass)){
                return tClass.cast(Integer.parseInt(str));
            }
            if(Long.class.equals(tClass)){
                return tClass.cast(Long.parseLong(str));
            }
            if(Float.class.equals(tClass)){
                return tClass.cast(Float.parseFloat(str));
            }
            if(Double.class.equals(tClass)){
                return tClass.cast(Double.parseDouble(str));
            }
            return tClass.cast(Double.parseDouble(str));
        }
        return tClass.cast(o);
    }

    public static long getValue(Long n){
        if(n == null){
            return 0;
        }
        return n;
    }

    public static void main(String[] args) throws ClassCastException {
        Class [] classes = {
                Byte.class,
                Short.class,
                Integer.class,
                Long.class,
                Float.class,
                Double.class,
                Number.class};
        for (Class aClass : classes) {
            System.out.println("===============测试 " + aClass.getName() + " ===============");
            System.out.println(getValue((byte)5, aClass));
            System.out.println(getValue((short)5, aClass));
            System.out.println(getValue(5, aClass));
            System.out.println(getValue((long)5, aClass));
            System.out.println(getValue((float)5.4, aClass));
            System.out.println(getValue(5.8, aClass));
            System.out.println(getValue("8", aClass));
            System.out.println(getValue(null, aClass));
        }

        System.out.println((double) ((float) 5.4));
//        System.out.println(getValue(true, Double.class));
//        System.out.println(getValue('a', Double.class));
//        System.out.println(getValue((Number)null, Double.class));
//        System.out.println(getValue(new DigitUtil(), Double.class));
    }
}
